import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 055. 二叉搜索树迭代器
 * @author: WXY
 * @create: 2023-01-04 19:46
 * @Version 1.0
 **/
public class offer_eighteen3_BSTIterator {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class BSTIterator {
        Queue<Integer> queue;

        public BSTIterator(TreeNode root) {
            queue = new ArrayDeque<>();
            dfs(root);
        }

        private void dfs(TreeNode root) {
            if (root == null) return;
            dfs(root.left);
            queue.offer(root.val);
            dfs(root.right);
        }

        public int next() {
            return queue.poll();
        }

        public boolean hasNext() {
            return queue.size() > 0;
        }
    }

}
